package com.capricornus.demo.controller;
import com.capricornus.demo.common.ApplicationException;
import com.capricornus.demo.common.ResponseResult;
import com.capricornus.demo.model.Department;
import com.capricornus.demo.model.DepartmentTree;
import com.capricornus.demo.service.user.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public ResponseResult<List<DepartmentTree>> list(){
            return ResponseResult.ok(deptService.selectDeptTreeList());
    }

    @PutMapping
    public ResponseResult<String>update(@RequestBody Department department) throws Exception{
        String deptId=department.getDeptId();
        Department department1= deptService.selectDeptInfo(deptId);
        if (department1==null){
            throw new ApplicationException(String.format("部门[%s]不存在", deptId));
        }
        deptService.update(department);
        return ResponseResult.ok("编辑部门成功");
    }



}
